/** 商品 图片上传 */

$(function() {
	
	/* 主图上传 */
	var $list_covers = $('#fileList_covers'), 
		$list_resources = $('#fileList_resources'), 
		ratio = window.devicePixelRatio || 1, 
		thumbnailWidth = 100 * ratio, 
		thumbnailHeight = 100 * ratio, 
		uploader_resources, 
		uploader_covers;

	
	uploader_covers = WebUploader.create({
		auto : true,
		swf : ctx+'/static/plugins/webuploader/0.1.5/Uploader.swf',
		server : ctx+'/file/uploadPic',
		pick : '#iconbutton',
		accept : {
			title : 'Images',
			extensions : 'gif,jpg,jpeg,bmp,png',
			mimeTypes : 'image/jpg,image/jpeg,image/png'
		}
	});

	uploader_covers.on('fileQueued',
			function(file) {
				var $li = $('<div id="' + file.id + '">' + '<img>'
						+ '<div class="info">' + file.name + '</div>'
						+ '</div>'), $img = $li.find('img');

				$list_covers.html($li);
				uploader_covers.makeThumb(file, function(error, src) {
					if (error) {
						$img.replaceWith('<span>不能预览</span>');
						return;
					}
					$img.attr('src', src);
				}, thumbnailWidth, thumbnailHeight);
			});

	uploader_covers.on('uploadSuccess', function(file, response) {
		$('#' + file.id).addClass('upload-state-done');
		$("#covers").val(response.datas);
	});

	uploader_covers.on('uploadError', function(file) {
		var $li = $('#' + file.id), $error = $li.find('div.error');
		if (!$error.length) {
			$error = $('<div class="error"></div>').appendTo($li);
		}

		$error.text('上传失败');
	});

	uploader_covers.on('uploadComplete', function(file) {
		$('#' + file.id).find('.progress').remove();
	});

	
	/* 图库上传 */
	uploader_resources = WebUploader.create({
		auto : true,
		swf : ctx+'/static/plugins/webuploader/0.1.5/Uploader.swf',
		server : ctx+'/file/uploadPic',
		pick : '#filePicker_resources',
		accept : {
			title : 'Images',
			extensions : 'gif,jpg,jpeg,bmp,png',
			mimeTypes : 'image/jpg,image/jpeg,image/png'
		}
	});

	uploader_resources.on('fileQueued',function(file) {
		var $li = $('<div id="' + file.id + '" class="file-item"><a onclick="delpic(this)">删除</a><br/>'
			+ '<img><div class="info"><input type="hidden" value="" name="resources" id="resource' + file.id + '"/></div></div>'), $img = $li.find('img');

		$list_resources.append($li);
		uploader_resources.makeThumb(file,function(error,src) {
			if (error) {
				$img.replaceWith('<span>不能预览</span>');
				return;
			}
			$img.attr('src',src);
		},
		thumbnailWidth,
		thumbnailHeight);
	});

	uploader_resources.on('uploadSuccess', function(file, response) {
/*		$('#' + file.id).addClass('upload-state-done');
		$("#resource" + file.id).val(response.datas);*/
		
		$("#image_list").val($("#image_list").val()+","+response.datas);
	});

	uploader_resources.on('uploadError',function(file) {
		var $li = $('#' + file.id), $error = $li
		.find('div.error');
		if (!$error.length) {
			$error = $('<div class="error"></div>').appendTo($li);
		}
		$error.text('上传失败');
	});

	uploader_resources.on('uploadComplete', function(
		file) {
		$('#' + file.id).find('.progress').remove();
	});
	//多图文
	
	
});

